// subPages/shopPage/shoppingCart/shoppingCart.js
var app = getApp();
var utils = require('../../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cartList:[],
    totalPrice:0,
    hasList:false,
    selectAllStatus:false,
    jsNum:0
  },
    //选择购物车商品
  selectGoods(e){
    let index = e.currentTarget.dataset.index;
    let carts = this.data.cartList;
    let selected = carts[index].selected;
    carts[index].selected = !selected;
      this.setData({
        cartList:carts
    })
    this.getTotalPrice();
  },
    //获取选择商品的总价值
    getTotalPrice() {
        let carts = this.data.cartList;                  // 获取购物车列表
        let total = 0;
        let jsNum = 0;
        for(let i = 0; i<carts.length; i++) {         // 循环列表得到每个数据
            if(carts[i].selected) {// 判断选中才会计算价格
                let num = parseInt(carts[i].num);
                let price = parseFloat(carts[i].content.price);
                total +=  num * price;
                ++jsNum;
            }
        }
        this.setData({                                // 最后赋值到data中渲染到页面
            carts: carts,
            totalPrice: total.toFixed(2),
            jsNum:jsNum
        });
    },
    //选择所以商品
    selectAll(e) {
        let selectAllStatus = this.data.selectAllStatus;    // 是否全选状态
        selectAllStatus = !selectAllStatus;
        let carts = this.data.cartList;

        for (let i = 0; i < carts.length; i++) {
            carts[i].selected = selectAllStatus;            // 改变所有商品状态
        }
        this.setData({
            selectAllStatus: selectAllStatus,
            cartList: carts
        });
        this.getTotalPrice();                               // 重新获取总价
    },
    // 增加数量
    addCount(e) {
        const index = e.currentTarget.dataset.index;
        let carts = this.data.cartList;
        let num = parseInt(carts[index].num);
        num = num + 1;
        carts[index].num = num;
        this.setData({
            cartList: carts
        });
        this.getTotalPrice();
    },
    // 减少数量
    minusCount(e) {
        const index = e.currentTarget.dataset.index;
        let carts = this.data.cartList;
        let num = carts[index].num;
        if(num <= 1){
            wx.showToast({
                title:'购买数量不能为0',
                icon:'none',
                duration:3000
            })
            return false;
        }
        num = num - 1;
        carts[index].num = num;
        this.setData({
            cartList: carts
        });
        this.getTotalPrice();
    },
    //商品移除购物车
    delGoods(e){
      var id = e.currentTarget.dataset.id;
      var that = this;
        wx.showModal({
            title: '提示',
            content: '您是否要将此商品移出购物车？',
            success(res) {
                if (res.confirm) {
                    utils.sendAjax(app.globalData.apiUrl+'/delete_buy','GET',function(res){
                        console.log(res);
                        if(res.data == 1){
                            wx.showToast({
                                title:'商品已移出购物车！',
                                icon:'none',
                                duration:2000
                            })
                            that.onShow();
                        }
                    },{id:id})
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })

    },
    //结算
    shoppingCartBuy(){
      var carts = this.data.cartList;
      var buyList = [];
      for(let i=0;i<carts.length;i++){
          if(carts[i].selected){
              buyList.push(carts[i]);
          }
      }
      if(buyList == ''){
          wx.showToast({
              title:'请选择需要购买的商品',
              icon:'none',
              duration:1000
          })
      }else{
          wx.setStorageSync('shoppingCartBuy',buyList);
          wx.navigateTo({
              url:'../shoppingCartOrder/shoppingCartOrder'
          })
      }

    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that = this
      console.log(app.globalData.userId);
      wx.setNavigationBarTitle({
         title:'购物车'
     })

  },
    addNum:function(){
        let that = this;
        that.setData({
            buyNum:that.data.buyNum+1
        })
        console.log(that.data.buyNum);
    },
    reduceNum:function(){
        let that = this;
        if(that.data.buyNum == 1){
            wx.showToast({
                title: '购买数量不能为0',
                icon: 'none',
                duration: 2000
            })
        }else{
            that.setData({
                buyNum:that.data.buyNum-1
            })
            console.log(that.data.buyNum);
        }
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
      var that = this;
      console.log(123);
      utils.sendAjax(app.globalData.apiUrl+'/get_user_buy','GET',function(res){
          console.log(res);
          that.setData({
              cartList:res.data
          })
      },{userid:app.globalData.userId})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
